//
//  DebugMainWindow.swift
//  YKSwiftDebugManager
//
//  Created by edward on 2021/12/1.
//

import UIKit

internal class DebugMainWindow: UIWindow {
    
    private var nav:UINavigationController? = nil;
    
    public static let share:DebugMainWindow = DebugMainWindow.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .clear
        self.windowLevel = (.statusBar - 1)
        self.isHidden = true
        if #available(iOS 13.0, *) {
            let scene = (UIApplication.shared.connectedScenes as NSSet).anyObject() as? UIScene
            if scene != nil {
                windowScene = (scene! as! UIWindowScene)
            }
        }
    }
    
    public func showOrDissMiss()->Void{
        if self.isHidden {
            //显示
            let debugVC = DebugMainViewController.init()
            self.setRoot(vc: debugVC)
            self.isHidden = false
        }else{
            //隐藏
            if self.rootViewController!.presentedViewController != nil {
                self.rootViewController!.presentedViewController?.dismiss(animated: false, completion: nil)
            }
            self.setRoot(vc: nil)
            self.isHidden = true
        }
    }
    
    private func setRoot(vc:UIViewController?)->Void
    {
        if vc != nil {
            let naVC = UINavigationController.init(rootViewController: vc!)
            let bgColor = UIColor.white
            if #available(iOS 15.0, *) {
                let standarAppearance = UINavigationBarAppearance.init()
                standarAppearance.backgroundColor = bgColor
                naVC.navigationBar.standardAppearance = standarAppearance
                
                let scrollEdgeAppearance = UINavigationBarAppearance.init()
                scrollEdgeAppearance.backgroundColor = bgColor
                naVC.navigationBar.scrollEdgeAppearance = scrollEdgeAppearance
            } else {
                naVC.navigationBar.barTintColor = bgColor
                naVC.navigationBar.backgroundColor = bgColor
            }
            let attributesDic:[NSAttributedString.Key:Any] = [NSAttributedString.Key.foregroundColor:UIColor.black,NSAttributedString.Key.font:UIFont.systemFont(ofSize: 18)]
            naVC.navigationBar.titleTextAttributes = attributesDic
            self.nav = naVC
            self.rootViewController = naVC
        }else{
            self.rootViewController = nil
            self.nav = nil
        }
    }
    

}
